package cn.ym.ymcc.mq;

import cn.ym.ymcc.constant.CustomConstant;
import cn.ym.ymcc.domain.MediaFile;
import cn.ym.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * mq的消费方，专门用来处理MediaFile，该类是把avi视频转为mp4,并且推流到视频服务器上面
 */
@Component
@RocketMQMessageListener(topic = CustomConstant.MQ_TOPIC_MEDIA,//topic
                        selectorExpression =CustomConstant.MQ_TAGS_MEDIA ,//tags
                        consumerGroup ="service-media-consumer" )//消费者组名
public class MediaFileConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        //获取消息内容
        byte[] body = messageExt.getBody();
        //获取具体的消息内容
        String jsonData = new String(body, StandardCharsets.UTF_8);
        //把json字符串转为MediaFile对象
        MediaFile mediaFile = JSONObject.parseObject(jsonData ,MediaFile.class);
        //把avi视频转为mp4和推流到服务器
        mediaFileService.handleFile2m3u8(mediaFile);
    }
}
